#! /usr/bin/python
# -*- coding: cp1250 -*-
#import sys; sys.path.append(r'C:/xxx/yyy')

import urllib2, os, string, re, time, codecs, sys

def get(url, target_file):
    '''
    to download html file
    get_url_file(url, target_file)
    '''
    try:
        response = urllib2.urlopen(url)
    except IOError, e:
        if hasattr(e, 'reason'):
            print 'network failure:', e.reason,
            msg = 'ERROR - network failure - ' + str(e.reason)
        elif hasattr(e, 'code'):
            print 'web server error:', e.code,
            msg = 'ERROR - web server error - ' + str(e.reason)
        else:
            print 'ERROR'
            msg = 'ERROR - ' + str(e.reason)
    else:
        html = response.read()
        msg = 'OK'
    if locals().has_key('html'):
        ofile = open(target_file, 'wb')
        ofile.write(html)
        ofile.close()
    return msg
        
